const mybehavior = require("../../../behaviors/requests")
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

import WxValidate from "../../../Wx_Validate/WxValidate"
Page({
  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/order_system",
    label: "",
    new_label: "",

  },
  // 扫描
  scan1() {
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          label: label_code
        })
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },
  scan2() {
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          new_label: label_code
        })
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },
  // 点击确定按钮
  formSubmit(e) {
    let that = this
    let params = {
      label: that.data.label,
      new_label: that.data.new_label,
    }
    //校验表单
    if (!that.WxValidate.checkForm(params)) {
      const error = that.WxValidate.errorList[0]
      Toast(error.msg)
    } else {
      //提交表单 
      var url = this.data.baseUrl + "/replace_label";
      this.post(url, params).then(res => {

        console.log("返回结果", res)
        if (res.code == 20000) {
          // this.setData({
          //   ccxq: "",
          // })
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }


  },

  //初始化表单验证规则
  initValidate() {
    let rules = {
      label: {
        required: true,
      },
      new_label: {
        required: true,
      },

    }

    let message = {
      label: {
        required: '原标签号不允许为空',
      },
      new_label: {
        required: '新标签号不允许为空',
      },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})